﻿using System;
using System.Collections.ObjectModel;
using WiseLinkWPF.Windows.Controls.Map;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Text;

namespace CCF20Module
{

    public static class BingMapHelper
    {
        public const string VEKey = "ApS-eaSGY-7exEJ4FXezUbxa-1Rh34NUQbKnVXe9H0vVWEqkHL_fdLO5HLdhUftp";
    }
    public class MyMapItem
    {
        public Location Location
        {
            get;
            set;
        }

        public string Title
        {
            get;
            set;
        }

        public string Description
        {
            get;
            set;
        }
    }
    
    public class MapItemsCollection : ObservableCollection<MyMapItem>
    {
    }
    public class OpenStreetCustomProvider : MapProviderBase
    {
        private const string TileMapnikUrlFormat = @"http://a.tile.openstreetmap.org/{zoom}/{x}/{y}.png";
        
        TileLayer tileLayer;


        private bool initialized;

        /// <summary>
        /// Initializes a new instance of the CustomOpenStreetMapProvider class.
        /// </summary>
        /// <param name="mode">Map mode.</param>
        /// <param name="labelVisible">Is labels visible.</param>
        public OpenStreetCustomProvider(MapMode mode, bool labelVisible)
            : base(mode, labelVisible)
        {
        }

        /// <summary>
        /// Initializes a new instance of the CustomOpenStreetMapProvider class.
        /// </summary>
        public OpenStreetCustomProvider()
            : this(MapMode.Road, true)
        {
        }

        /// <summary>
        /// Gets value which indicates whether labels are supported by the map provider.
        /// </summary>
        public override bool IsLabelSupported
        {
            get
            {
                return false;
            }
        }

        /// <summary>
        /// Returns the SpatialReference for the map provider.
        /// </summary>
        public override ISpatialReference SpatialReference
        {
            get
            {
                return new MercatorProjection();
            }
        }

        /// <summary>
        /// Gets list of the supported map modes.
        /// </summary>
        /// <returns>List of the supported map modes.</returns>
        public override IEnumerable<MapMode> SupportedModes
        {
            get
            {
                yield return MapMode.Road;
            }
        }

        protected string TileXYToQuadKey(int tileX, int tileY, int levelOfDetail)
        {
            var quadKey = new StringBuilder();

            for (int i = levelOfDetail; i > 0; i--)
            {
                char digit = '0';
                int mask = 1 << (i - 1);

                if ((tileX & mask) != 0)
                    digit++;

                if ((tileY & mask) != 0)
                {
                    digit++;
                    digit++;
                }

                quadKey.Append(digit);
            }

            return quadKey.ToString();
        }
        /// <summary>
        /// Gets the image URI.
        /// </summary>
        /// <param name="tileLevel">Tile level.</param>
        /// <param name="tilePositionX">Tile X.</param>
        /// <param name="tilePositionY">Tile Y.</param>
        /// <returns>URI of image.</returns>
        public override Uri GetTile(int tileLevel, int tilePositionX, int tilePositionY)
        {
            int zoomLevel = ConvertTileToZoomLevel(tileLevel);
            string QuadKey =  TileXYToQuadKey(tilePositionX, tilePositionY, zoomLevel);
            //string url = TileMapnikUrlFormat;
            string url = "http://r1.tiles.ditu.live.com/tiles/r{quad}.png?g=66";


            url = ProtocolHelper.SetScheme(url);
            url = url.Replace("{quad}", QuadKey);
            //url = url.Replace("{zoom}", zoomLevel.ToString(CultureInfo.InvariantCulture));
            //url = url.Replace("{x}", tilePositionX.ToString(CultureInfo.InvariantCulture));
            //url = url.Replace("{y}", tilePositionY.ToString(CultureInfo.InvariantCulture));

            return new Uri(url);
        }

        /// <summary>
        /// Initialize provider.
        /// </summary>
        public override void Initialize()
        {
            this.initialized = base.IsInitialized;
        }

        /// <summary>
        /// Gets value which indicates whether given mode is supported by map provider.
        /// </summary>
        /// <param name="mode">Map mode to check.</param>
        /// <returns>true if given mode is supported. Otherwise - false.</returns>
        public override bool IsModeSupported(MapMode mode)
        {
            return true;
        }

        /// <summary>
        /// MapModeChanged handler.
        /// </summary>
        protected override void OnMapModeChanged(MapMode oldMode, MapMode newMode)
        {
        }


    }
}
